<?php

namespace app\api\controller;


use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;
use AlibabaCloud\Tea\Utils\Utils\RuntimeOptions;
use app\api\model\codemodel;
use app\api\model\Usermodel;
use app\api\validate\UserValidate;
use app\BaseController;
use Darabonba\OpenApi\Models\Config;
use think\Validate;

class User extends BaseController
{
    public function index(){
        $data = input('post.');
        $validate = new UserValidate();
        $check = $validate->scene('registration')->check($data);
        if (!$check){
           return error($validate->getError());
        }
        $exit = Usermodel::where('mobile',$data['mobile'])->find();
        if ($exit){
            return error('该账户已存在');
        }
        if (!checkSmsCode($data['mobile'], $data['code'])) {
            return error('验证码错误或已过期');
        }
        $salt = RandStar();
        $password = $data['password'].$salt;
        $data['password']= md5($password);
        $data['salt'] = $salt;
        $model = Usermodel::create($data);
        if ($model){
            return success('注册成功',$data);
        }else{
            return error('注册失败');
        }
    }
        // 阿里云短信发送示例
        // 文档地址：https://api.aliyun.com/api-tools/sdk/Dysmsapi?version=2017-05-25&language=php-tea&tab=primer-doc
        // 新用户优惠购：https://t.aliyun.com/U/3Fupv7
        public function sendAliSms()
        {
            $code = mt_rand(1111, 9999);
            $mobile = input('mobile');
            $config = new Config([
                // 从阿里云获取
                'accessKeyId' => config('app.aliSms.accessKeyId'),
                'accessKeySecret' => config('app.aliSms.accessKeySecret'),
            ]);

            $config->endpoint = "dysmsapi.aliyuncs.com";
            $client = new Dysmsapi($config);

            // 运行时参数超时设置，仅对使用了该运行时参数实例的请求有效
            $runtimeOptions = new RuntimeOptions();
            // true 忽略证书校验
            $runtimeOptions->ignoreSSL = true;

            $sendSmsRequest = new SendSmsRequest();
            // 手机号
            $sendSmsRequest->phoneNumbers = $mobile;
            // 签名名称
            $sendSmsRequest->signName = '阿里云短信测试';
            // 模板CODE
            $sendSmsRequest->templateCode = 'SMS_154950909';
            // 生成随机数作为验证码
            $sendSmsRequest->templateParam = json_encode(['code' => $code]);
            try {
                $response = $client->sendSmsWithOptions($sendSmsRequest, $runtimeOptions);
            } catch (\Exception $exception) {
                halt($exception->getMessage());
            }
            if ($response->body->code == 'OK') {
                echo '短信发送成功！';
                $data = [
                    'mobile' =>$mobile,
                    'code' => $code,
                    'send_time'=>time(),
                    ];
                $model = codemodel::create($data);
                if ($model){
                    return success('存储成功');
                }
            } else {
                halt($response->body->message);
            }

    }
}